var utils = require('../../utils/utils.js');
var app = getApp();
const domainUrl = app.globalData.domainUrl
var curPage = 1;
var isPullDownRefreshing = false;
var method;
var openid;
var type;

Page({

  data: {
    hidden: true
  },


  onLoad: function (options) {
    console.log(options);
    openid = wx.getStorageSync('my_openid');
	method = options.mtd;
	type = options.type;
  curPage = 1;
    //var defaultUrl = domainUrl + options.mtd + '?openId='+openid+'&type='+options.type+'&pageIndex='+curPage;
    //utils.http(defaultUrl, this.processDoubanData);
    this.showCardList();
    this.showCardListTotal();
  },
  
  onReachBottom:function(){
    console.log('onPullDownRefresh...');
    isPullDownRefreshing = true;
    this.showCardList();
    this.showCardListTotal();
  },

  showCardListTotal: function () {
    var that = this;
    wx.request({
      url: domainUrl + 'shuihuCardListTotalByOpenIdAndType?openId=' + openid+"&type="+type,
      success: function (res) {
        console.log(res.data)
        that.setData({
          total: res.data.total,
          have: res.data.have
        });
      }
    })
  },

  showCardList: function () {
    this.setData({
      hidden: false
    });
	var url = domainUrl + method + '?openId='+openid+'&type='+type+'&pageIndex='+curPage;
    utils.http(url, this.processDoubanData);
  },

  processDoubanData: function (bookList) {
	wx.showNavigationBarLoading();
	var that = this;
    var books = [];
    console.log(bookList);
    for (var idx in bookList.data) {
      var book = bookList.data[idx];
      var title = book.name;
      var temp = {
        title: book.idx+"-"+title,
        coverImg: book.url
      }
      books.push(temp);
    }
	
	if(curPage == 1)
		that.setData({
			books: books,
			hidden: true,
		});
	else
		that.setData({
			books: that.data.books.concat(books),
			hidden: true,
		});
	
	curPage = curPage + 1;
	wx.hideNavigationBarLoading();
		if(isPullDownRefreshing)
			wx.stopPullDownRefresh();
    console.log(books);
  },
  //之前用来从豆瓣获取数据的方法
  getBooksList: function (url) {
    this.setData({
      hidden: false
    });
    var that = this;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/xml'
      },
      success: function (res) {
        //console.log(res.data);
		that.processDoubanData(res.data);
      }
    })
  },

  previewPhoto: function (event) {
    console.log(event);
    var curTarget = event.currentTarget.dataset.imageSrc;
    console.log(curTarget);
    wx.navigateTo({
      url: '../preview/preview?imagepath=' + curTarget
    })
  },

  onShareAppMessage: function () {

  }
})